<template>
	<view>

		<view class="flex padding">
			<view class="flex-sub  margin-xs radius text-center">
				<button class="walls" style="background-image: url('../../static/my/wall1.png');">{{type}}</button>
				<text class="text-yellow  uni-h6 ">{{type1_use||0}}</text>
			</view>
			<view class="flex-sub margin-xs radius text-center" @tap="cc">
				<image src="../../static/my/transfers.png" mode="aspectFit" style="width: 50px;height: 50px;"></image>

			</view>
			<view class="flex-sub margin-xs radius text-center">
				<button class="walls" style="background-image: url('../../static/my/wall2.png');">{{type2}}</button>
				<text class="text-yellow  uni-h6 ">{{type2_use||0}}</text>
			</view>
		</view>



		<!-- <view style="margin: 10px;">
			<view class="cu-form-group">
				<view class="title">币种选择</view>
				<picker @change="PickerChange2" :value="index2" :range="picker">
					<view class="picker">
						{{picker[index2]}}
					</view>
				</picker>
			</view>
		</view> -->
		<!--
		<view class="cu-form-group">
			<view class="title">币种选择</view>
			<picker @change="PickerChange" :value="type" :range="picker">
				<view class="picker">
					{{type}}
				</view>
			</picker>
		</view>
		<view class="cu-form-group">
				<view class="title">币种选择</view>
				<picker @change="PickerChange2" :value="type2" :range="picker">
					<view class="picker">
						{{type2}}
					</view>
				</picker>
			</view> -->

		<view style="margin: 10px;">
			<view class="cu-form-group t-t1">
				<view class="title">{{i18n.transfers.number}}</view>

			</view>
			<view class="cu-form-group t-t2">

				<input :placeholder="i18n.transfers.inputnumber" name="input" v-model="nums" @tap="chight" @blur="lost"></input>
			</view>
		</view>


		<!-- <view class="cu-form-group">
				<view class="solid-bottom text-xs  ">
					<text class="text-gray">可用数量：900 {{type}}</text>
				</view>
			</view> -->
		<!-- <view class="padding flex flex-direction">
				<button class="cu-btn bg-yellow lg" @tap="sub">提交</button>
			</view>
 -->



		</form>
		<view class="padding flex flex-direction">
			<button class="cu-btn bg-yellow lg" @tap="sub">{{i18n.transfers.confirm}}</button>
		</view>
		<key-words :show_key="show_key" :show_subTitle="false" :price="'1.00'" @closeFuc="closeFuc" @getPassword="getPassword"></key-words>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				// picker: ['BTC', 'ETH', 'EMP'],
				type: 'EMP',
				type2: 'EMP',
				addr: {
					BTC: '0X122456432465661',
					ETH: '0X142452323222261',
					EMP: '0X132456433265661',
				},
				nums: '',
				binfo: {},
				tradetimer: [],
				rate: {
					EMP2BJ: 0,
					BJ2EMP: 0,
				},
				inc: false,
				show_key: false,
				check: false,
				index: 0,
				index2: 0,
				ptype: '',
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
				myconfig: state => state.user.myconfig
			}),
			i18n() {
				return this.$t('message');
			},
			hasLogin() {
				return this.userinfo.id ? true : false
			},
			picker() {
				let arr = [];
				for (let key in this.myconfig.transfers) {
					arr.push(key)
				}
				return arr;
			},
			type1_use() {
				return this.userinfo[this.myconfig.transfers[this.type]];
			},
			type2_use() {
				return this.userinfo[this.myconfig.transfers[this.type2]];
			},
			// type1_freeze() {
			// 	return this.userinfo[this.myconfig.transfers[this.type] + 'freeze'];
			// },
			// type2_freeze() {
			// 	return this.userinfo[this.myconfig.transfers[this.type2] + 'freeze'];
			// },
			type1_total() {
				let wall = this.myconfig.transfers[this.type];
				return this.accAdd(this.userinfo[wall], this.userinfo[wall + 'freeze'])
			},
			type2_total() {
				let wall = this.myconfig.transfers[this.type2];
				return this.accAdd(this.userinfo[wall], this.userinfo[wall + 'freeze'])
			}

		},
		onLoad(options) {
			uni.setNavigationBarTitle({
				title: this.i18n.transfers.title
			});
			this.ptype = options.index ? options.index : 'wall1'
		},
		onShow() {
			console.log(this.tradetimer.length)
			console.log(this.ptype)
			switch (this.ptype) {
				case "wall1":
					this.type = this.picker[0];
					this.type2 = this.picker[1];
					this.picker.splice(0,1)
					break;
				case "wall2":
					this.type = this.picker[1];
					this.type2 = this.picker[0];
					this.picker.splice(1,1)
					console.log(this.picker)
					break;
				case "wall3":
					this.type = this.picker[2];
					this.type2 = this.picker[0];
					this.picker.splice(2,1)
					break;
				default:
					this.type = this.picker[1];
					this.type2 = this.picker[0];
					this.picker.splice(1,1)
			}

			this.init();
		},
		onBackPress() {
			this.cleartimer();
		},
		methods: {
			...mapActions(['getUserinfo']),
			init() {
				this.binfo = uni.getStorageSync('binfo');

			},
			chight() {
				this.inc = true;
			},
			lost() {
				this.inc = false;
			},
			PickerChange(e) {
				this.type = this.picker[e.detail.value];
				this.index = e.detail.value
			},
			PickerChange2(e) {
				this.type2 = this.picker[e.detail.value];
				this.index2 = e.detail.value
			},
			//加法
			accAdd(arg1, arg2) {
				var r1, r2, m, n;
				try {
					r1 = arg1.toString().split(".")[1].length
				} catch (e) {
					r1 = 0
				}
				try {
					r2 = arg2.toString().split(".")[1].length
				} catch (e) {
					r2 = 0
				}
				m = Math.pow(10, Math.max(r1, r2));
				//last modify by deeka
				//动态控制精度长度
				n = (r1 >= r2) ? r1 : r2;
				var ree = ((arg1 * m + arg2 * m) / m).toFixed(n);
				return ree > 0 ? ree : 0;
			},
			cc() {
				let eee = this.type;
				this.type = this.type2;
				this.type2 = eee;
			},
			sub() {
				if (this.nums <= 0) {
					this.$api.msg('数量不能为0')
					return false;
				}
				if (parseInt(this.type1_use) < parseInt(this.nums)) {
					this.$api.msg('超过最大可用数量')
					return false;
				}
				// if (!this.check) {
				// 	this.show_key = true;
				// 	return 1;
				// }

				let send = {
					type1: this.myconfig.transfers[this.type],
					type2: this.myconfig.transfers[this.type2],
					nums: this.nums,
					EMP2BJ: this.rate.EMP2BJ,
					BJ2EMP: this.rate.BJ2EMP,
				}

				//提交
				this.$http.post('Transfer.coin2coin', send).then(re => {
					console.log(re)
					if (re.msg) {
						this.$api.msg(re.msg)
						this.getUserinfo();
					}
				})
			},
			closeFuc() {
				this.show_key = false;
			},
			//密码输入完需要执行的方法
			getPassword(msg) {
				//msg:密码   例如:{msg:123456}
				console.log(msg)
				let data = {
					password2: msg.password
				}
				this.$http.post('user.checkpass', data).then(res => {
					console.log(res)
					if (res.code == 1) {
						this.check = true;
						this.sub();
					} else {
						this.$api.msg(res.msg);
					}
				})
				this.closeFuc();
			},
		}
	}
</script>

<style>
	.cu-form-group uni-input {
		font-weight: 500;
		color: #ffffff;
	}

	.row {
		display: inline;
	}

	.tij {
		bottom: 0px;
		width: -webkit-fill-available;
	}

	.walls {
		background-size: 100% 100%;
		height: 58px;
		background-color: rgb(51, 51, 51);
		line-height: 58px;
		font-size: 14px;
		font-weight: bold;
	}
</style>
